package com.zzh.phone.hviews.widget;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;

import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;

/**
 * Created by ZZH on 2022/11/15.
 *
 * @Date: 2022/11/15
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description:
 */
public class HTabLayout extends TabLayout {
    private int sTabSelectedIndicatorColor = Color.TRANSPARENT;
    public static final int INDICATOR_GRAVITY_BOTTOM_RIGHT = 4;

    public HTabLayout(@NonNull Context context) {
        super(context);
    }

    public HTabLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HTabLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        int indicatorHeight = getTabSelectedIndicator().getBounds().height();
        if (indicatorHeight < 0) {
            indicatorHeight = getTabSelectedIndicator().getIntrinsicHeight();
        }

        int indicatorTop = 0;
        int indicatorBottom = 0;
        int indicatorRight = 0;
        int indicatorLeft = 0;

        switch (getTabIndicatorGravity()) {
            case INDICATOR_GRAVITY_BOTTOM:
                indicatorTop = getHeight() - indicatorHeight;
                indicatorBottom = getHeight();
                break;
            case INDICATOR_GRAVITY_CENTER:
                indicatorTop = (getHeight() - indicatorHeight) / 2;
                indicatorBottom = (getHeight() + indicatorHeight) / 2;
                break;
            case INDICATOR_GRAVITY_TOP:
                indicatorTop = 0;
                indicatorBottom = indicatorHeight;
                break;
            case INDICATOR_GRAVITY_STRETCH:
                indicatorTop = 0;
                indicatorBottom = getHeight();
                break;
            case INDICATOR_GRAVITY_BOTTOM_RIGHT:
                indicatorTop = getHeight() - indicatorHeight;
                indicatorBottom = getHeight();
                indicatorLeft = 0;
                indicatorRight = 0;

                break;
            default:
                break;
        }

        // Ensure the drawable actually has a width and is worth drawing
        if (getTabSelectedIndicator().getBounds().width() > 0) {
            // Use the left and right bounds of the drawable, as set by the indicator interpolator.
            // Update the top and bottom to respect the indicator gravity property.
            Rect indicatorBounds = getTabSelectedIndicator().getBounds();

            Drawable indicator = getTabSelectedIndicator();
            Log.d("-----", "layout left: " + indicatorBounds.left +
                    ", top: " + indicatorTop +
                    ", right: " + indicatorBounds.right +
                    ", bottom: " + indicatorBottom);
            Log.d("-----", "indicator left: " + indicatorBounds.left +
                    ", top: " + indicatorBounds.top +
                    ", right: " + indicatorBounds.right +
                    ", bottom: " + indicatorBounds.bottom);
            Log.d("-----", "indicator2 MinimumHeight: " + indicator.getMinimumHeight() +
                    ", MinimumWidth: " + indicator.getMinimumWidth() +
                    ", IntrinsicHeight: " + indicator.getIntrinsicHeight() +
                    ", IntrinsicWidth: " + indicator.getIntrinsicWidth());




            getTabSelectedIndicator().setBounds(
                    indicatorBounds.right-indicator.getMinimumWidth(), indicatorTop, indicatorBounds.right, indicatorBottom);
            if (sTabSelectedIndicatorColor != Color.TRANSPARENT) {
                // If a tint color has been specified using TabLayout's setSelectedTabIndicatorColor, wrap
                // the drawable and tint it as specified.
                indicator = DrawableCompat.wrap(indicator);
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
                    indicator.setColorFilter(sTabSelectedIndicatorColor, PorterDuff.Mode.SRC_IN);
                } else {
                    DrawableCompat.setTint(indicator, sTabSelectedIndicatorColor);
                }
            } else {
                // Remove existing tint if setSelectedTabIndicatorColor to Color.Transparent.
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
                    indicator.setColorFilter(null);
                } else {
                    DrawableCompat.setTintList(indicator, null);
                }
            }

            indicator.draw(canvas);
        }

        // Draw the tab item contents (icon and label) on top of the background + indicator layers
        super.draw(canvas);
    }


    @Override
    public void setSelectedTabIndicatorColor(int color) {
        sTabSelectedIndicatorColor = color;
        super.setSelectedTabIndicatorColor(color);
    }
}

